package com.fxu.gen;

import cn.hutool.core.util.StrUtil;
import com.fxu.gen.enums.QueryType;
import com.fxu.gen.enums.SignEnum;
import com.fxu.gen.enums.TplEnum;
import com.fxu.tpl.entity.Column;
import com.fxu.tpl.entity.Table;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Entity类的分页请求
 * @author fangxu
 * @version 1.0.0
 */
public final class PageReq extends Base {

	public PageReq(Table table, File destFile) {
		super(table, TplEnum.PageReq, destFile);
	}
	
	@Override
	public String doReplace() {
		String fileTxt = super.doReplace();
		fileTxt = replace(fileTxt, SignEnum.PageReqColumn, getColumn());
		fileTxt = fileTxt.replace(fileTxt.substring(fileTxt.indexOf("@ApiModel("), fileTxt.indexOf("\")") + 2), getApiModel());
		fileTxt = replaceImports(fileTxt, false); //放最后才能收集imports
		return fileTxt;
	}
	
	private String getApiModel() {
		return String.format("@ApiModel(value = \"%s\", description = \"%s\")", tableBean + "PageReq", table.getCmmt() + "分页请求");
	}

	private StringBuffer getColumn() {
		StrBuf buffer = new StrBuf();

		for (Column column : columns) {
			// 含有集合字段 、大数据字段不做入参查询
			if (Controller.isNotQuery(column) || column.getName().endsWith("_ids")
					|| (column.getName().endsWith("s") && (column.getCmmt().endsWith("集合") || column.getCmmt().endsWith("集")))) {
				continue;
			}

			String clnName = AutoUtil.firstLowerBean(column.getName());
			QueryType queryType = QueryType.find(column.getQueryType());
			String typeText = column.getTypeText();
			if (column.isText()) {
				typeText = "String";
			}
			if (column.isDecimal()) {
				typeText = "BigDecimal";
			}
			if (Entity.hasLimits(column.getLimits())) {
				String enumName = Entity.getEnumName(tableBean, column);
				if (!enumName.startsWith(tableBean)) {
					AutoUtil.toImports(imports, corePackage + ".enums." + enumName);
				} else {
					AutoUtil.toImports(imports, this.thisPackage.replace(".dto", ".enums.") + enumName);
				}
				typeText = enumName;
			}

			AutoUtil.toImports(imports, column.isDecimal() ? BigDecimal.class : null);
			AutoUtil.toImports(imports, column.isDate() ? Date.class : null);
			AutoUtil.toImports(imports, column.isFloat() ? Float.class : null);
			AutoUtil.toImports(imports, column.isDouble() ? Double.class : null);
			buffer.append(1, getApiModelProperty(column));
			buffer.append(1, "private {1} {2};\n", typeText, clnName);

			// 大于、小于处理
			if (queryType == QueryType.RANGE_NOT_EQUALS || queryType == QueryType.RANGE_EQUALS) {
				if (column.isDate()) {
					buffer.append(1, getApiModelProperty(column));
					buffer.append(1, "private {1} {2};\n", typeText, clnName + "Start");

					buffer.append(1, getApiModelProperty(column));
					buffer.append(1, "private {1} {2};\n", typeText, clnName + "End");
				} else if (column.isNumber() || column.isDecimal()) {
					buffer.append(1, getApiModelProperty(column));
					buffer.append(1, "private {1} {2};\n", typeText, clnName + "Max");

					buffer.append(1, getApiModelProperty(column));
					buffer.append(1, "private {1} {2};\n", typeText, clnName + "Min");
				}
			}
		}
		return buffer.toBuffer();
	}

	private static String getApiModelProperty(Column column) {
		String tpl = "@ApiModelProperty(value = \"{1}\"{2})";
		String comment = column.getCmmt();
		if (Entity.hasLimits(column.getLimits()) && !comment.contains(column.getLimits())) {
			comment += "[" + column.getLimits() + "]";
		}
		if (column.getQueryType() != null && column.getQueryType() > 1) {
			comment += "[" + column.getQueryTypeText() + "搜索]";
		}
		return StrBuf.format(tpl, comment, "");
	}

}
